package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.UserAction;
import com.jokerpan.project.service.UserActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("userAction")
@Api("用户操作Api")
public class UserActionController {

    @Resource
    UserActionService userActionService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有用户操作")
    public ListResult<Object> queryList(UserAction userAction) {
        return ResultBuilder.buildListSuccess(this.userActionService.queryList(userAction));
    }

    @GetMapping("/queryChartData")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询趋势图数据")
    public ListResult<Object> queryChartData(UserAction userAction) {
        return ResultBuilder.buildListSuccess(this.userActionService.queryChartData(userAction));
    }

    @PostMapping("/add")
    @ApiOperation("新增用户操作")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    public ActionResult addAction(@RequestBody UserAction userAction){
        if (this.userActionService.add(userAction)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed("操作失败！");
        }
    }

    @GetMapping("/cancelFollow")
    @ApiOperation("取消关注操作")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    public ActionResult cancelFollow(@RequestParam("userId") Integer userId, @RequestParam("flowUserId") Integer flowUserId){
        if (this.userActionService.cancelFollow(userId, flowUserId)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed("操作失败！");
        }
    }
}
